﻿using Clothes.Comment;
using Clothes.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Clothes.IDAL;
using Clothes.DAL;
using Clothes.IBLL;

namespace Clothes.BLL
{
    public class CLUserBLL:CLUserIBLL
    {
        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>返回用户信息</returns>
        public OperResult<CLUser> LoginByUserName(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<CLUser>.Failed("用户名和密码不能为空！");
            }
            // 通过用户名查询用户
            CLLoginIDAL userService = new CLLoginDAL();
            CLLogin loginEntity = userService.FindByUserName(userName);
            if (loginEntity == null)
            {
                return OperResult<CLUser>.Failed("用户名不存在！");
            }
            // 验证用户密码
            Encryption shaEncryption = new Encryption();
            if (!shaEncryption.SHA1Encryp(password).Equals(loginEntity.Password))
            {
                return OperResult<CLUser>.Failed("用户名或密码错误！");
            }
            // 查找用户信息
            CLUserIDAL profileService = new CLUserDAL();
            CLUser profileEntity = profileService.FindByUserId(loginEntity.Id);
            // 返回结果
            return OperResult<CLUser>.Success(profileEntity);
        }

        /// <summary>
        /// 通过手机号登录
        /// </summary>
        /// <param name="mobileNumber"></param>
        /// <param name="password"></param>
        /// <returns>返回用户信息</returns>
        public OperResult<CLUser> LoginByMobileNumber(string mobileNumber, string password)
        {
            if (string.IsNullOrWhiteSpace(mobileNumber) || string.IsNullOrWhiteSpace(password))
            {
                return OperResult<CLUser>.Failed("手机号和密码不能为空！");
            }
            // 通过用户名查询用户
            CLLoginIDAL userService = new CLLoginDAL();
            CLLogin loginEntity = userService.FindByMobileNumber(mobileNumber);
            if (loginEntity == null)
            {
                return OperResult<CLUser>.Failed("手机号未注册！");
            }
            // 验证用户密码
            Encryption shaEncryption = new Encryption();
            if (!shaEncryption.SHA1Encryp(password).Equals(loginEntity.Password))
            {
                return OperResult<CLUser>.Failed("手机号或密码错误！");
            }
            // 查找用户信息
            CLUserIDAL profileService = new CLUserDAL();
            CLUser profileEntity = profileService.FindByUserId(loginEntity.Id);
            // 返回结果
            return OperResult<CLUser>.Success(profileEntity);
        }
        CLUserIDAL cLUser = new CLUserDAL();
        public CLUser Select(int id)
        {
            
            return cLUser.Tiao(a=>a.CLLoginId==id);
        }

        public bool SelectEdit(int id, string xing, string di,string shou)
        {
            return cLUser.SelectEdit(id,xing,di,shou);
        }


        public bool InsertBLL(CLUser cL)
        {
            return cLUser.Insert(cL);
        }
    }
}
